﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using XiaoMan.Model;

namespace XiaoMan.Common.API
{
    /// <summary>
    /// 钉钉bug提醒机器人
    /// </summary>
    public class DingBugRobertApi
    {
        public static void SendMessage(Exception e)
        {
            string msg ="错误信息："+ e.Message + "\r\n" +"错误位置："+ e.StackTrace.Replace(@"\", "/");
            string content = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"" + msg + "\"}}";
            Task.Factory.StartNew(() => { Post(ProjectConst.ALDingBugRobertUrl, content); });
        }
        private static void Post(string url, string content)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json; charset=utf-8";
            byte[] data = Encoding.UTF8.GetBytes(content);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
        }
    }
}
